﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Redbrick.Silverlight;
using Moria.Silverlight.Controls;
using DeenGames.Utils;
using System.Diagnostics;
using Redbrick.Silverlight.Common;

namespace Moria.Silverlight.Views
{
	public partial class NewGame : MoriaPage
	{

		#region Consts & Settings


		#endregion Consts & Settings

		#region Declarations

		private static Lazy<Uri> myUri = new Lazy<Uri>(() => new Uri("/NewGame.xaml", UriKind.Relative));


		#endregion Declarations

		#region Constructor

		public NewGame()
		{
			this.DebugMethodInfo();
			InitializeComponent();
			Loaded += new RoutedEventHandler(this.PageLoaded);
		}

		void PageLoaded(object sender, RoutedEventArgs e)
		{
			this.DebugMethodInfo(sender, e);

			((DelegateCommand)this.Moria.SaveGameCommand).CanExecuteHandler = (ob) => false;
			((DelegateCommand)this.Moria.NewGameCommand).Executed += new System.EventHandler<Redbrick.Silverlight.CommandArgs>(this.NewGame_Executed);

			this.Moria.RestoreGameCommand.Executed += new System.EventHandler<CommandArgs>(this.RestoreGame_Executed);
			this.Moria.ViewScoresCommand.Executed += new System.EventHandler<CommandArgs>(this.ViewScores_Executed);

			if (this.Moria.FileList.Count == 0)
			{
				((DelegateCommand)this.Moria.RestoreGameCommand).CanExecuteHandler = (ob) => false;
			}
		}

		#endregion Constructor

		#region Methods


		#endregion Methods

		#region Events

		// Executes when the user navigates to this page.
		protected override void OnNavigatedTo(NavigationEventArgs e)
		{
			base.OnNavigatedTo(e);
		}

		void ViewScores_Executed(object sender, CommandArgs args)
		{
			this.DebugMethodInfo(sender, args);
			ViewScoresDialog dlg = new ViewScoresDialog();
			dlg.DataContext = this.DataContext;
			dlg.Closed += (s, e) => this.IsEnabled = true;
			dlg.Show();
			this.IsEnabled = false;
		}
		void RestoreGame_Executed(object sender, CommandArgs e)
		{
			this.DebugMethodInfo(sender, e);
			LoadGameDialog dlg = new LoadGameDialog();
			dlg.DataContext = this.DataContext;
			dlg.Closed += new System.EventHandler(this.RestoreGame_Closed);
			dlg.Show();
			this.IsEnabled = false;
		}
		void NewGame_Executed(object sender, CommandArgs e)
		{
			this.DebugMethodInfo(sender, e);
			NewGameDialog dlg = new NewGameDialog();
			dlg.DataContext = this.DataContext;
			dlg.Closed += new System.EventHandler(this.NewPlayer_Closed);
			dlg.DataContext = Moria.Player;
			dlg.Show();
			this.IsEnabled = false;
		}

		void NewPlayer_Closed(object sender, System.EventArgs e)
		{
			this.DebugMethodInfo(sender, e);
			this.IsEnabled = true;
			NewGameDialog dlg = sender as NewGameDialog;
			if (dlg != null)
			{
				if ((bool)dlg.DialogResult)
				{
					this.Moria.Player.IsGenerated = true;
					this.Moria.dun_level = 0;
					this.NavigationService.Navigate(GameBoard.Uri);
				}
			}
		}
		void RestoreGame_Closed(object sender, System.EventArgs e)
		{
			this.DebugMethodInfo(sender, e);
			this.IsEnabled = true;
			LoadGameDialog dlg = sender as LoadGameDialog;
			if (dlg != null)
			{
				if ((bool)dlg.DialogResult)
				{
					Moria.RestoreTheGame(dlg.SaveFileName);
					this.NavigationService.Navigate(GameBoard.Uri);
				}
			}
		}

		#endregion Events

		#region Properties

		public static Uri Uri { get { return myUri.Value; } }

		#endregion Properties
	}
}
